<template>
  <div>
    <el-dialog
      title="人员信息"
      :visible.sync="dialogVisible"
      width="600px"
      destroy-on-close
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="人员" prop="student.id">
              <el-select
                v-model="form.student.id"
                filterable
                remote
                :loading="loading"
                :remote-method="searchItems"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { studentService, roomassignService } from "@/api/dormService";

export default {
  dicts: [],
  components: {},

  data() {
    return {
      loading: false,
      resolve: null,
      dialogVisible: false,

      options: [],

      // 表单参数
      form: {
        student: {
          id: "",
        },
        room: {
          id: "",
        },
      },

      // 表单校验
      rules: {
        name: [
          { required: true, message: "不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "用户名称长度必须介于 2 和 50 之间",
            trigger: "blur",
          },
        ],
        "student.id": [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  created() {},
  methods: {
    async searchItems(query) {
      if (query) {
        this.loading = true;
        try {
          const response = await studentService.listDynamic({ name: query });
          console.log("搜索结果", response);
          this.options = (response.data.data || []).map((el) => ({
            label: [el.name, el.idcard].join("-"),
            value: el.id,
          }));
        } catch (error) {
          console.error("搜索请求失败", error);
        } finally {
          this.loading = false;
        }
      } else {
        this.options = [];
      }
    },
    open(row) {
      // console.log(row);
      this.options = [];
      this.dialogVisible = true;
      this.form = {
        student: {
          id: "",
        },
        room: {
          id: "",
        },
      };
      row && (this.form.room.id = row.id);
      return new Promise((resolve) => {
        this.resolve = resolve;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          roomassignService.postSave(this.form).then((response) => {
            this.$modal.msgSuccess("操作成功");
            this.dialogVisible = false;
            // this.getList();
            this.resolve(response.data);
          });
        }
      });
    },

    // 取消按钮
    cancel() {
      this.dialogVisible = false;
      // this.reset();
    },
  },
};
</script>

<style lang="scss" scoped></style>
